package cn.iocoder.yudao.module.book.controller.config;

import cn.iocoder.yudao.module.book.util.MinioUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

@RestController
@RequestMapping("/api/minio")
public class MinioController {

    @Autowired
    private MinioUtils minioUtils;

    /**
     * 上传单个文件
     */
    @PostMapping("/upload/single")
    public String uploadSingleFile(@RequestParam("file") MultipartFile file) {
        MultipartFile[] files = new MultipartFile[]{file};
        List<String> fileNames = minioUtils.upload(files);
        if (!fileNames.isEmpty()) {
            return "文件上传成功，文件名: " + fileNames.get(0);
        } else {
            return "文件上传失败";
        }
    }

    /**
     * 上传多个文件
     */
    @PostMapping("/upload/multiple")
    public String uploadMultipleFiles(@RequestParam("files") MultipartFile[] files) {
        List<String> fileNames = minioUtils.upload(files);
        if (!fileNames.isEmpty()) {
            return "文件上传成功，文件名列表: " + String.join(", ", fileNames);
        } else {
            return "文件上传失败";
        }
    }
}